<?php

namespace app\admin\controller\fastflow\flow;

use app\admin\model\AuthRule;
use app\common\controller\Backend;


/**
 * 系统设置
 *
 * @icon fa fa-handshake-o
 */
class Config extends Backend
{

    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = new \app\admin\model\fastflow\flow\Config;
    }

    public function index()
    {
        $config_rows = $this->model->select();
        if ($this->request->isAjax()) {
            $params = input('row/a');
            if ($params == null) {
                $this->error('暂无可用配置项，请待后续版本更新');
            }
            foreach ($params as $key => $value) {
                for ($i = 0; $i < count($config_rows); $i++) {
                    if ($config_rows[$i]['name'] == $key) {
                        $config_rows[$i]['value'] = $value;
                        $config_rows[$i]->save();
                        break;
                    }
                }
                if ($key == 'billshowway') {
                    $this->handleBillShowWay($value);
                }
            }
            $this->success('操作成功');
        }
        $config_temp = [];
        foreach ($config_rows as &$config) {
            $config = $config->toArray();
            $config_temp[$config['name']] = $config;
        }
        $this->assign(['config_rows' => $config_temp]);
        return $this->view->fetch();
    }

    private function handleBillShowWay($value)
    {
        $authRuleModel = new AuthRule();
        $bill_rule = $authRuleModel->where('name', 'fastflow/bill')->find();
        $billlist_rule = $authRuleModel->where('name', 'fastflow/billlist')->find();
        if ($billlist_rule['ismenu'] == 0) {
            $billlist_rule['ismenu'] = 1;
            $billlist_rule->save();
        }
        if ($value == 'menu') {
            if ($bill_rule) {
                $bill_rule->status = 'normal';
                $bill_rule->save();
                $billlist_rule->status = 'hidden';
                $billlist_rule->save();
            }
        } elseif ($value == 'page') {
            if ($bill_rule) {
                $bill_rule->status = 'hidden';
                $bill_rule->save();
                $billlist_rule->status = 'normal';
                $billlist_rule->save();
            }
        }
    }

    public function handleBillShowWayAjax()
    {
        if ($this->request->isAjax()) {
            $params = input('');
            if (isset($params['billshowway'])) {
                $value = $params['billshowway'];
                $this->handleBillShowWay($value);
                $billShowWayRow = $this->model->where('name','billshowway')->find();
                if ($billShowWayRow) {
                    $billShowWayRow['value'] = $value;
                    $billShowWayRow->save();
                }
                $this->success('操作成功');
            }
            $this->error('参数错误');
        }
        $this->error('非法访问');
    }

}
